<template>
  <div>
    <!------------------------面包屑--------------------------->
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px">
      <el-breadcrumb-item :to="{path:'/blank'}"><i class="el-icon-guide" style="color: black"></i>班级管理</el-breadcrumb-item>
      <el-breadcrumb-item :to="{path:'/class'}">班级管理</el-breadcrumb-item>
      <el-breadcrumb-item>班级详情</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>
    <!------------------------表格开始--------------------------->
    <el-table
        :data="ClassData"
        stripe
        border
        style="width: 100%">
      <el-table-column
          prop="id"
          label="学生ID"
          width="80"
          align="center">
      </el-table-column>
      <el-table-column
          prop="classId"
          label="班级ID"
          width="80"
          align="center">
      </el-table-column>
      <el-table-column
          prop="trueName"
          label="姓名"
          align="center"
          show-overflow-tooltip
          width="180">
      </el-table-column>
      <el-table-column
          header-align="center"
          prop="majorName"
          show-overflow-tooltip
          label="专业名称">
      </el-table-column>
      <el-table-column
          width="240"
          align="center"
          prop="className"
          show-overflow-tooltip
          label="班级">
      </el-table-column>
      <el-table-column
          label="操作"
          width="150"
          align="center">
        <template slot-scope="scope">
          <el-button icon="el-icon-edit" size="mini" type="primary" circle @click="openEditDialog(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
<!--       &#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;分页器&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;
    <div style="text-align: center; margin: 10px auto">
      <el-pagination
          @current-change="changePage"
          layout="total, prev, pager, next"
          :total="total"
          :current-page="currentPage"
          :page-size="pageSize">
      </el-pagination>
    </div>-->
    <!--    修改弹出框-->
    <div style="margin: 0 auto">
      <el-dialog title="修改班级" :visible.sync="dialogFormVisible" width="480px">
        <el-form :model="editForm" :rules="rules" ref="ruleForm" label-width="100px" style="margin: -10px 0 -28px 0">
          <el-form-item label="学生id" prop="userId">
            <el-input disabled v-model="editForm.userId" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="学生名字" prop="userName">
            <el-input disabled v-model="editForm.userName" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="班级id" prop="id">
            <el-input disabled v-model="editForm.id" readonly autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="修改班级名称" prop="id" style="text-align: left">
            <el-select v-model="editForm.id" placeholder="请选择班级">
              <el-option v-for="item in classEasyList" :key="item.id" :label="item.className" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="submitEditForm">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
let localJwt = localStorage.getItem('localJwt');
export default {
  data() {
    return {
      ClassData: [],
      total: 0,
      pageSize: 0,
      currentPage: 1,
      cid:1,
      classEasyList:[],
      rules: {
        className: [
          {required: true, message: '请输入名称', trigger: 'blur'},
          {min: 2, max: 25, message: '长度在 2 到 25 个字符', trigger: 'blur'}
        ],
      },
      //Dialog属性
      dialogFormVisible: false,
      editForm: {
        /*班级id*/
        id: '',
        /*学生id*/
        userId:'',
        /*学生（真实）名字*/
        userName: '',
      },
    }
  },
  methods: {
    openEditDialog(grade){
      let url = 'http://localhost:9284/class/standard?id=' + grade.classId;
      console.log('url =' + url);

      this.axios
          .create({
            'headers': {
              'Authorization': localJwt
            }
          })
          .get(url).then((response) =>{
        let jsonResult = response.data;
        console.log(jsonResult);
        if (jsonResult.state == 20000){
          this.editForm = jsonResult.data;
          this.editForm.userId = grade.id;
          this.editForm.userName = grade.trueName;
          this.dialogFormVisible = true;
          if (jsonResult.jwt!=null){
            localStorage.setItem("localJwt",jsonResult.jwt);
          }
        }else if(jsonResult.state==70100){
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
          this.$router.push('/')
        } else {
          this.$alert(jsonResult.message,'错误',{
            confirmButtonText:'确认',
            callback: action => {// eslint-disable-line no-unused-vars
              this.loadAlbumList();
            }
          })
        }
      });
    },
    submitEditForm() {
      let url = 'http://localhost:9284/class/details/update';
      console.log('url=' + url);

      let formData = this.qs.stringify(this.editForm);
      console.log('formData = ' + formData);

      this.axios
          .create({
            'headers': {
              'Authorization': localJwt
            }
          })
          .post(url, formData).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.$message({
            message: '修改成功！',
            type: 'success'
          });
          this.dialogFormVisible = false;
          this.loadList(this.cid);
          if (jsonResult.jwt!=null){
            localStorage.setItem("localJwt",jsonResult.jwt);
          }
        }else if(jsonResult.state==70100){
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
          this.$router.push('/')
        }  else if (jsonResult == 40400) {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {// eslint-disable-line no-unused-vars
              this.dialogFormVisible = false;
              this.loadList(this.cid);
            }
          });
        } else {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {// eslint-disable-line no-unused-vars
            }
          });
        }
      });
    },
    loadclassList() {
      let page = this.$router.currentRoute.query.page;
      if (!page) {
        page = 1;
      }
      console.log('localJwt =' + localJwt);
      let url = 'http://localhost:9284/class/details/list?page=' + page;
      console.log('url =' + url);

      this.axios
          .create({
            'headers': {
              'Authorization': localJwt
            }
          })
          .get(url).then((response) => {
        let jsonResult = response.data;
        console.log(jsonResult.state);
        console.log(jsonResult.data.list);
        if (jsonResult.state == 20000) {
          this.ClassData = jsonResult.data.list;
          this.total = jsonResult.data.total;
          this.pageSize = jsonResult.data.pageSize;
          this.currentPage = jsonResult.data.currentPage;
          if (jsonResult.jwt!=null){
            localStorage.setItem("localJwt",jsonResult.jwt);
          }
        }else if(jsonResult.state==70100){
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
          this.$router.push('/')
        }  else {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确认',
            callback: action => {// eslint-disable-line no-unused-vars
            }
          })
        }
      });
    },
    loadList(cid){
      console.log('localJwt =' + localJwt);
      let url = 'http://localhost:9284/class/details/listByClassId?id='+cid
      console.log('url =' + url);
      this.axios
          .create({
            'headers': {
              'Authorization': localJwt
            }
          })
          .get(url).then((response) => {
        let jsonResult = response.data;
        console.log(jsonResult.state);
        console.log(jsonResult.data);
        console.log(jsonResult.list);
        if (jsonResult.state == 20000) {
          this.ClassData = jsonResult.data;
          if (jsonResult.jwt!=null){
            localStorage.setItem("localJwt",jsonResult.jwt);
          }
        }else if(jsonResult.state==70100){
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
          this.$router.push('/')
        }  else {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确认',
            callback: action => {// eslint-disable-line no-unused-vars
            }
          })
        }
      });
    },
    loadEasyClassList(){
      // http://localhost:9284/class/details/easy-list
      let url = 'http://localhost:9284/class/details/easy-list';
      console.log('加载班级下拉选')
      console.log('url =' + url);
      this.axios
          .create({
            'headers': {
              'Authorization': localJwt
            }
          })
          .get(url).then((response) => {
        let jsonResult = response.data;
        console.log(jsonResult.state);
        console.log(jsonResult.data);
        if (jsonResult.state == 20000) {
          this.classEasyList = jsonResult.data;
          console.log('加载下拉选完成')
          if (jsonResult.jwt!=null){
            localStorage.setItem("localJwt",jsonResult.jwt);
          }
        }else if(jsonResult.state==70100){
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
          this.$router.push('/')
        }  else {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确认',
            callback: action => {// eslint-disable-line no-unused-vars
            }
          })
        }
      });

    }
  },

  mounted() {
      console.log('开始加载页面')
      this.loadEasyClassList();
      if (location.search.indexOf("classId")!=-1){
        this.ClassData.classId =location.search.split("=")[1];
        console.log("班级id = " + this.ClassData.classId);
        this.cid = this.ClassData.classId
        this.loadList(this.cid);
        console.log("班级id = " + this.cid);
      }else {
        console.log('------')
        this.loadclassList();
      }
  },

}
</script>

<style scoped>

</style>
